using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections.ObjectModel;

namespace MyspaceToolkit
{
    public class MySpaceXMLObjectList<T> : Collection<T> where T : MySpaceXMLObject, new()
    {
        public void LoadXML(string xml)
        {
            XmlDocument xd = MySpaceXMLObject.LoadXMLDocument(xml);

            XmlNode el = xd.GetElementsByTagName(GetNodeName())[0];

            XmlNodeList el_list = (el as XmlElement).GetElementsByTagName("user");

            foreach (XmlNode xn in el_list)
            {
                T t = new T();
                t.LoadXMLNode(xn as XmlElement);
                Add(t);
            }
        }

        protected virtual string GetNodeName()
        {
            throw new Exception("GetNodeName() not implemented");
        }
    }

    public class MySpaceXMLObject
    {
        public static XmlDocument LoadXMLDocument(string xml)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xml);

            // _nsManager = new XmlNamespaceManager(xmlDocument.NameTable);
            // _nsManager.AddNamespace("Facebook", Resources.FacebookNamespace);

            return xmlDocument;
        }

        public void LoadXMLNode(XmlElement xn)
        {
            foreach (System.Reflection.PropertyInfo f in this.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
            {
                XmlNodeList el = xn.GetElementsByTagName(f.Name.ToLower());

                if (el != null && el.Count > 0)
                {
                    f.SetValue(this, el[0].InnerText, null);
                }
            }
        }

        public void LoadXML(string xml)
        {
            XmlDocument xd = MySpaceXMLObject.LoadXMLDocument(xml);

            // XmlNode node = xd.SelectSingleNode(GetNodeName());

            LoadXMLNode(xd.FirstChild as XmlElement);
        }

        protected virtual string GetNodeName()
        {
            throw new Exception("GetNodeName() not implemented");
        }
    }

    public class MySpaceUserList : MySpaceXMLObjectList<MySpaceUser>
    {
        protected override string GetNodeName()
        {
            return "friends";
        }
    }

    public class MySpaceUser : MySpaceXMLObject
    {
        private string _userid, _displayname, _weburi, _imageuri, _largeimageuri, _usertype;

        public string Usertype
        {
            get { return _usertype; }
            set { _usertype = value; }
        }

        public string Largeimageuri
        {
            get { return _largeimageuri; }
            set { _largeimageuri = value; }
        }

        public string Imageuri
        {
            get { return _imageuri; }
            set { _imageuri = value; }
        }

        public string Weburi
        {
            get { return _weburi; }
            set { _weburi = value; }
        }

        public string Displayname
        {
            get { return _displayname; }
            set { _displayname = value; }
        }

        public string Userid
        {
            get { return _userid; }
            set { _userid = value; }
        }

        protected override string GetNodeName()
        {
            return "user";
        }

        public MySpaceUser()
        {
        }
    }

    public class MySpaceProfile : MySpaceXMLObject
    {
        string _profileuri, _city, _region, _postalcode, _country, _hometown, _age, _gender, _culture, _aboutme, _maritalstatus;

        public string Aboutme
        {
            get { return _aboutme; }
            set { _aboutme = value; }
        }

        public string Culture
        {
            get { return _culture; }
            set { _culture = value; }
        }

        public string Gender
        {
            get { return _gender; }
            set { _gender = value; }
        }

        public string Age
        {
            get { return _age; }
            set { _age = value; }
        }

        public string Hometown
        {
            get { return _hometown; }
            set { _hometown = value; }
        }

        public string Country
        {
            get { return _country; }
            set { _country = value; }
        }

        public string Postalcode
        {
            get { return _postalcode; }
            set { _postalcode = value; }
        }

        public string Region
        {
            get { return _region; }
            set { _region = value; }
        }

        public string City
        {
            get { return _city; }
            set { _city = value; }
        }

        public string Profileuri
        {
            get { return _profileuri; }
            set { _profileuri = value; }
        }

        public string Maritalstatus
        {
            get { return _maritalstatus; }
            set { _maritalstatus = value; }
        }

        protected override string GetNodeName()
        {
            return "Profile";
        }

        public MySpaceProfile()
        {
        }
    }
}
